package pacificlms.Verifiable.Database;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import pacificlms.MasterConnection;
import pacificlms.Verifiable.*;

/**
 * CourseReserve is a child of VerifiableObject dealing with Course Reserves
 * Since all fields are get/set they are not individually commented
 * @author Brian Hume
 */
public class CourseReserve extends VerifiableObject
{
    @Override
    public String TableName() {
        return "CourseReserve";
    }
    
    private String ResourceCode;
    @VerifyAnnotation(ApplicationName="Resource Code", DBName="ResourceCode",  Type=TypeEnum.LOOKUP, Length=50, PrimaryKey=true, LookupDBTable="Resource", LookupDBField="ResourceCode")
    public String ResourceCode(String value)
    {
        return ResourceCode = StandardProcess(ResourceCode, value);
    }
    
    
    private String IdentityNumber;
    @VerifyAnnotation(ApplicationName="Identity Number", DBName="IdentityNumber",  Type=TypeEnum.NUMERICSTRING, Length=8, ExactLength=true, PrimaryKey=true, LookupDBTable="Member", LookupDBField="IdentityNumber")
    public String IdentityNumber(String value)
    {
        return IdentityNumber = StandardProcess(IdentityNumber, value);
    }
    
    
    private String StartDate;
    @VerifyAnnotation(ApplicationName="Start Date", DBName="StartDate",  Type=TypeEnum.DATE, Length=50)
    public String StartDate(String value)
    {
        return StartDate = StandardProcess(StartDate, value);
    }
    
    
    private String EndDate;
    @VerifyAnnotation(ApplicationName="End Date", DBName="EndDate",  Type=TypeEnum.DATE, Length=50, NotNull=false)
    public String EndDate(String value)
    {
        return EndDate = StandardProcess(EndDate, value);
    }
    
    
    private String Course;
    @VerifyAnnotation(ApplicationName="Course #", DBName="Course",  Type=TypeEnum.STRING, Length=8)
    public String Course(String value)
    {
        return Course = StandardProcess(Course, value);
    }
    
    @Override
    public String toString()
    {
        String Str = "[" + ResourceCode +"] Reserved by faculty #" + IdentityNumber + " for " + Course + " from " + StartDate;
        if (EndDate != null && !EndDate.equals(""))
        {
            Str += " until " + EndDate;
        }
        return Str;
    }
    
    public boolean IsReserved()
    {
        try
        {
            Connection con = MasterConnection.getInstance();
            Statement stmt = con.createStatement();
            String SQL = this.FindIdenticalSQL();
            ResultSet rs=stmt.executeQuery(SQL);
            GregorianCalendar now= new GregorianCalendar();
            SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
            String CurrentDate=sf.format(now.getTime());
            Date EndDate;
            boolean reserved=false;
            //System.out.println("null or not?:"+this.ResourceCode(null));
            while(rs.next())
            {
                System.out.println("res:"+rs.getString("ResourceCode"));
                EndDate=rs.getDate("EndDate");
                if(EndDate == null || sf.parse(CurrentDate).before(EndDate))
                {
                    reserved=true;
                    break;
                }
            }
            return reserved;
        }
        catch(Exception Ex)
        {
            JOptionPane.showMessageDialog(null, "Database error, please try again later\n" + Ex.getMessage(), "Database Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } 
        
    }
}
